package abd.pr2.bd;

import javax.xml.namespace.QName;
import javax.xml.xquery.XQDataSource;
import javax.xml.xquery.XQException;
import javax.xml.xquery.XQPreparedExpression;
import javax.xml.xquery.XQResultSequence;

import net.xqj.exist.ExistXQDataSource;

import org.w3c.dom.Element;

import abd.pr2.model.Noticia;

public class ConsultaNoticiasPorCategoria extends ConsultaAbstracta<Noticia> {
	
	private static final String ARCHIVO_CONSULTA_XQUERY = "1.xquery";
	private String categoria;
	
	public ConsultaNoticiasPorCategoria(XQDataSource xqds, String categoria) {
		super(xqds);
		this.categoria = categoria;
		this.nombreArchivo = ARCHIVO_CONSULTA_XQUERY;
	}
	
	public static void main(String[] args){
		
		XQDataSource xqs = new ExistXQDataSource();
		try {
			xqs.setProperty("serverName", "localhost");
			xqs.setProperty("port", "8080");
		} catch (XQException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		new ConsultaNoticiasPorCategoria(xqs, "tecnología").ejecuta();
	}

	@Override
	public void addBinds(XQPreparedExpression exp) throws XQException {
		exp.bindString(new QName("etiqueta"), this.categoria, null);
	}

	@Override
	public Noticia buildObject(XQResultSequence rs) throws XQException {
		org.w3c.dom.Element	elementosXML = (org.w3c.dom.Element)rs.getObject();
		return new Noticia(elementosXML.getAttribute("id"),elementosXML.getAttribute("titular"));
	}
	
}
